unit uFraPlayers;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, StrUtils, gnugettext,
  Oracle, Grids, Buttons, editplayers, uSQLs, editplayernotes, umainutils,
  GIFImg, GR32_Image;

type
  TFraPlayers = class(TFrame)
    pnl_SubjectImage: TPanel;
    img_Player: TImage;
    Label2: TLabel;
    btn_ChangePicture: TButton;
    btn_DeletePicture: TButton;
    btn_PhotoFromWebCam: TButton;
    Panel8: TPanel;
    stg_Players: TStringGrid;
    pnl_Users: TPanel;
    btn_New: TSpeedButton;
    btn_Edit: TSpeedButton;
    btn_Duplicate: TSpeedButton;
    btn_Delete: TSpeedButton;
    Panel1: TPanel;
    stg_PlayerNotes: TStringGrid;
    Panel2: TPanel;
    SpeedButton1: TSpeedButton;
    SpeedButton2: TSpeedButton;
    SpeedButton3: TSpeedButton;
    SpeedButton4: TSpeedButton;
    Label11: TLabel;
    Label1: TLabel;
    lbl_UsersSum: TLabel;
    edt_SearchPlayer: TLabeledEdit;
    btn_SearchNext: TButton;
    edt_PPoints: TLabeledEdit;
    procedure btn_PhotoFromWebCamClick(Sender: TObject);
    procedure btn_DeletePictureClick(Sender: TObject);
    procedure btn_ChangePictureClick(Sender: TObject);
    procedure stg_PlayersSelectCell(Sender: TObject; ACol, ARow: Integer; var CanSelect: Boolean);
    procedure btn_NewClick(Sender: TObject);
    procedure btn_EditClick(Sender: TObject);
    procedure btn_DuplicateClick(Sender: TObject);
    procedure btn_DeleteClick(Sender: TObject);
    procedure stg_PlayerNotesSelectCell(Sender: TObject; ACol, ARow: Integer; var CanSelect: Boolean);
    procedure SpeedButton1Click(Sender: TObject);
    procedure SpeedButton2Click(Sender: TObject);
    procedure SpeedButton3Click(Sender: TObject);
    procedure SpeedButton4Click(Sender: TObject);
    procedure btn_CloseClick(Sender: TObject);
    procedure stg_PlayersDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
    procedure stg_PlayerNotesDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
    procedure edt_SearchPlayerChange(Sender: TObject);
    procedure btn_SearchNextClick(Sender: TObject);
    procedure stg_PlayersClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  fraPlayers: TFraPlayers;

implementation

uses uFrmMain, uUtils, uFrmCameraGetImage, uFrmInsertNewPlayer;
{$R *.dfm}

procedure TFraPlayers.btn_ChangePictureClick(Sender: TObject);
var
  FileExt: string;
begin
  FrmMain.OpenPictureDialog1.Execute();
  FileExt := ReplaceStr(UpperCase(ExtractFileExt(FrmMain.OpenPictureDialog1.FileName)), '.', '');
  // ---
  FrmMain.SavePhotoToDB(FrmMain.OpenPictureDialog1.FileName, FileExt, '');
  img_Player.Picture := uUtils.GetImage(SQL_Player_Picture + stg_Players.Cells[0, stg_Players.Row], FrmMain.OracleSession1, true);
end;

procedure TFraPlayers.btn_CloseClick(Sender: TObject);
begin
  FrmInsertNewPlayer.Close;
end;

procedure TFraPlayers.btn_DeleteClick(Sender: TObject);
begin
  if MessageDlg(_('Delete record?'), mtWarning, [mbYes, mbNo], 0) = mrYes then
  begin
    DeleteDataFromDB('PLAYERS', 'ID=' + stg_Players.Cells[0, stg_Players.Row]);
    FrmMain.GetGridData(stg_Players, SQL_Players, false);
  end;
end;

procedure TFraPlayers.btn_DeletePictureClick(Sender: TObject);
var
  SubjectID: string;
begin
  SubjectID := stg_Players.Cells[0, stg_Players.Row];
  if MessageDlg(_('Delete image?'), mtWarning, [mbYes, mbNo], 0) = mrYes then
  begin
    UpdateDataToDB('PLAYERS', 'IMAGE='''', IMAGE_TYPE=''''', 'ID=' + SubjectID);
  end;
  img_Player.Picture := nil;
end;

procedure TFraPlayers.btn_DuplicateClick(Sender: TObject);
begin
  uFrmEditPlayers.ShowForm(true, true, stg_Players.Cells[0, stg_Players.Row], false);
end;

procedure TFraPlayers.btn_EditClick(Sender: TObject);
begin
  uFrmEditPlayers.ShowForm(true, false, stg_Players.Cells[0, stg_Players.Row], false);
end;

procedure TFraPlayers.btn_NewClick(Sender: TObject);
begin
  uFrmEditPlayers.ShowForm(false, false, stg_Players.Cells[0, stg_Players.Row], false);
end;

procedure TFraPlayers.btn_PhotoFromWebCamClick(Sender: TObject);
begin
  FrmMain.ShowCAMphotoForm(stg_Players.Cells[0, stg_Players.Row]);
  if (FrmCameraGetImage.FrmSubjectImage.img_TakenPhoto.Picture <> nil) and (FrmCameraGetImage.ImageValid) then
  begin
    img_Player.Picture := FrmCameraGetImage.FrmSubjectImage.img_TakenPhoto.Picture;
  end;
end;

procedure TFraPlayers.btn_SearchNextClick(Sender: TObject);
begin
  FrmMain.SearchStringGrid(stg_Players, edt_SearchPlayer.Text, stg_Players.Row);
end;

procedure TFraPlayers.edt_SearchPlayerChange(Sender: TObject);
begin
  FrmMain.SearchStringGrid(stg_Players, edt_SearchPlayer.Text);
end;

procedure TFraPlayers.SpeedButton1Click(Sender: TObject);
begin
  uFrmEditPlayerNotes.ShowForm(false, false, stg_PlayerNotes.Cells[0, stg_PlayerNotes.Row]);
end;

procedure TFraPlayers.SpeedButton2Click(Sender: TObject);
begin
  uFrmEditPlayerNotes.ShowForm(true, false, stg_PlayerNotes.Cells[0, stg_PlayerNotes.Row]);
end;

procedure TFraPlayers.SpeedButton3Click(Sender: TObject);
begin
  uFrmEditPlayerNotes.ShowForm(true, true, stg_PlayerNotes.Cells[0, stg_PlayerNotes.Row]);
end;

procedure TFraPlayers.SpeedButton4Click(Sender: TObject);
begin
  if MessageDlg(_('Delete record?'), mtWarning, [mbYes, mbNo], 0) = mrYes then
  begin
    DeleteDataFromDB('PLAYER_NOTES', 'ID=' + stg_PlayerNotes.Cells[0, stg_PlayerNotes.Row]);
    FrmMain.GetGridData(stg_PlayerNotes, SQL_Player_Notes, false);
  end;
end;

procedure TFraPlayers.stg_PlayerNotesDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
begin
  FrmMain.GridDrawCell(Sender, ACol, ARow, Rect, State);
end;

procedure TFraPlayers.stg_PlayerNotesSelectCell(Sender: TObject; ACol, ARow: Integer; var CanSelect: Boolean);
begin
  if (ARow > 1) and (stg_PlayerNotes.Cells[0, ARow] <> '') then
  begin
    SpeedButton2.Enabled := true;
    SpeedButton3.Enabled := true;
    SpeedButton4.Enabled := true;
  end
  else
  begin
    SpeedButton2.Enabled := false;
    SpeedButton3.Enabled := false;
    SpeedButton4.Enabled := false;
  end;
end;

procedure TFraPlayers.stg_PlayersClick(Sender: TObject);
begin
  stg_Players.Repaint;
  // edt_SearchPlayer.Text := '';
end;

procedure TFraPlayers.stg_PlayersDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
begin
  if (ARow = 1) and (ACol = 1) then
  begin
    lbl_UsersSum.Caption := 'Players count:' + ' ' + IntToStr(stg_Players.RowCount - 1);
  end;
  FrmMain.GridDrawCell(Sender, ACol, ARow, Rect, State);
end;

procedure TFraPlayers.stg_PlayersSelectCell(Sender: TObject; ACol, ARow: Integer; var CanSelect: Boolean);
begin
  FrmMain.SearchGridStr := '';
  btn_ChangePicture.Enabled := false;
  btn_DeletePicture.Enabled := false;
  btn_PhotoFromWebCam.Enabled := false;
  btn_Edit.Enabled := false;
  btn_Duplicate.Enabled := false;
  btn_Delete.Enabled := false;
  SpeedButton1.Enabled := false;
  SpeedButton2.Enabled := false;
  SpeedButton3.Enabled := false;
  SpeedButton4.Enabled := false;
  if (ARow > 1) and (stg_Players.Cells[0, ARow] <> '') then
  begin
    img_Player.Picture := uUtils.GetImage(SQL_Player_Picture + stg_Players.Cells[0, ARow], FrmMain.OracleSession1, true);
    FrmMain.GetGridData(stg_PlayerNotes, SQL_Player_Notes + stg_Players.Cells[0, ARow], false);
    stg_PlayerNotes.ColWidths[2] := stg_PlayerNotes.Width - stg_PlayerNotes.ColWidths[1];
    FrmMain.OracleQuery1.Close;
    FrmMain.OracleQuery1.SQL.Text := 'SELECT PP FROM GET_PP_PLAYER WHERE PLAYER_ID=' + stg_Players.Cells[0, ARow];
    FrmMain.OracleQuery1.Execute;
    if FrmMain.OracleQuery1.Eof then
      edt_PPoints.Text := '0'
    ELSE
      edt_PPoints.Text := FloatToStrF(FrmMain.OracleQuery1.Field(0), ffNumber, 10, 0);
    btn_ChangePicture.Enabled := true;
    btn_DeletePicture.Enabled := true;
    btn_PhotoFromWebCam.Enabled := true;
    img_Player.Transparent := true;
    btn_Edit.Enabled := true;
    btn_Duplicate.Enabled := true;
    btn_Delete.Enabled := true;
    SpeedButton1.Enabled := true;
  end;
end;

end.
